package com.superhelper.superhelper_web.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.common.domain.AuserShop;
import com.superhelper.superhelper_web.domain.AUserRole;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.AuserShopAudio;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Role;

/**
 * AdminMapper
 * 
 * @author Administrator
 *
 */

public interface AdminMapper {

	/**
	 * 从数据库获取用户对象
	 * 
	 * @param username
	 * @return
	 */
	@Select("select * from auser where username = #{username}")
	Admin getAdminByUsername(@Param("username") String username);

	@Update("update auser set loginAt=#{time} where id=#{id}")
	int updateLoginAt(@Param("id") Integer id,@Param("time")Date time);

	/**
	 * 从数据库获取用户对象
	 * 
	 * @param username
	 * @return
	 */
	@Select("select * from auser where id = #{id}")
	Admin getAdminById(@Param("id") Integer id);

	@Select("select count(1) from auser where roleCode<#{roleCode} or createId=#{createId}")
	int getAdminCount(@Param("roleCode") int roleCode,
			@Param("createId") int createId);

	@Select("select * from auser where roleCode<#{roleCode} or createId=#{createId} order by id desc limit #{page.startRow},#{page.pageSize}")
	List<Admin> getAdmin(@Param("roleCode") int roleCode,
			@Param("createId") int createId, @Param("page") PageModel pageModel);

	@Select("select * from role order by id desc limit #{page.startRow},#{page.pageSize}")
	List<Role> getRole(@Param("page") PageModel pageModel);

	@Select("select count(1) from role")
	int getRoleCount();

	@Select("select * from auser_role where auserId=#{userId}")
	List<AUserRole> getRoleIdByUserId(@Param("userId") Integer userId);

	@Select("select * from role")
	List<Role> getAllAuRoles();

	/**
	 * 添加管理员
	 * 
	 * @return
	 */
	@Options(useGeneratedKeys = true, keyProperty = "a.id")
	@Insert("insert into auser(username,email,pwd,createAt,updateAt,roleCode,status)values(#{a.username},#{a.email},md5(#{a.pwd}),now(),now(),#{a.roleCode},#{a.status})")
	int addAdmin(@Param("a") Admin admin);

	/**
	 * 添加角色
	 * 
	 * @return
	 */
	@Options(useGeneratedKeys = true, keyProperty = "a.id")
	@Insert("insert into role(name,remark,createAt,updateAt)values(#{a.name},#{a.remark},now(),now())")
	int addRole(@Param("a") Role role);

	@Select("select * from role where id = #{id}")
	Role getRoleById(@Param("id") Integer id);

	// 绑定角色
	@Insert("insert into auser_role(auserId,roleId)values(#{userId},#{roleId})")
	int addUserRole(@Param("userId") Integer userId,
			@Param("roleId") Integer roleId);

	@Update("update auser set username=#{admin.username},email=#{admin.email},pwd=#{admin.pwd} where id=#{admin.id}")
	int updateAdmin(@Param("admin") Admin admin);

	@Update("update role set name=#{a.name},remark=#{a.remark} where id=#{a.id}")
	int updateRole(@Param("a") Role role);

	/**
	 * 删除管理员
	 */
	@Delete("delete from auser where id=#{id}")
	int delAdminById(Integer id);

	/**
	 * 删除角色
	 */
	@Delete("delete from role where id=#{id}")
	int delRoleById(Integer id);

	@Delete("delete from auser_role where auserId=#{userId}")
	int deleteAuserRoleByUserId(@Param("userId") Integer userId);

	@Delete("delete from auser_role where roleId=#{roleId}")
	int deleteAuserRoleByRoleId(@Param("roleId") Integer roleId);

	/**
	 * 禁止登录管理员
	 * 
	 * @return
	 */
	@Update("update auser set status=#{status} where id=#{id}")
	int updateAdminStatus(@Param("id") Integer id,
			@Param("status") Integer status);

	/**
	 * 更新管理员密码
	 * 
	 * @return
	 */
	@Update("update auser set pwd=md5(#{pwd}) where id=#{id}")
	int updateAdminPwd(@Param("id") Integer id, @Param("pwd") String pwd);

	@Select("select * from auser_shop where auserId=#{auserId}")
	List<AuserShop> selectList(@Param("auserId") Integer auserId);

	@Select("select * from auser_shop where shopId=#{shopId} and shop_type=#{shop_type}")
	AuserShop getAuserShopByShopId(@Param("shopId") String shopId,
			@Param("shop_type") String shop_type);

	// 绑定商铺
	@Options(useGeneratedKeys = true, keyProperty = "a.id")
	@Insert("insert into auser_shop(auserId,shopId,shop_type,token,state)values(#{a.auserId},#{a.shopId},#{a.shop_type},#{a.token},#{a.state})")
	int addAuserShop(@Param("a") AuserShop a);

	@Update("update auser_shop set token=#{token},state=1 where shopId=#{shopId} and shop_type=#{shop_type}")
	int updateShopToken(@Param("shopId") String shopId,
			@Param("shop_type") String shop_type, @Param("token") String token);

	@Select("select * from auser_shop where auserId=#{auserId}")
	List<AuserShop> getAuserShopsByAuserId(@Param("auserId") Integer auserId);

	@Update("update auser_shop set lon=#{lon},lat=#{lat} where shopId=#{shopId} and shop_type=#{shop_type}")
	int updateShopPoi(@Param("shopId") String shopId,
			@Param("shop_type") String shop_type, @Param("lat") Double lat,
			@Param("lon") Double lon);

	@Update("update auser_shop set orderAuto=#{orderAuto} where id=#{id}")
	int updateOrderAuto(@Param("id") Integer id,
			@Param("orderAuto") int orderAuto);

	@Select("select * from auser_shop_audio where auserId=#{auserId} order by seq asc")
	List<AuserShopAudio> getAuserAudioByAuserId(
			@Param("auserId") Integer auserId);

	@Update("update auser_shop set deliverType=#{deliverType},deliverUid=#{deliverUid},deliverShopNo=#{deliverShopNo} where id=#{id}")
	int updateDeliverSwitch(@Param("id") Integer id,
			@Param("deliverType") int deliverType,
			@Param("deliverUid") String deliverUid,
			@Param("deliverShopNo") String deliverShopNo);

	@Insert("replace into auser_shop_audio (auserId,title,type,count,src,seq)value(#{a.auserId},#{a.title},#{a.type},#{a.count},#{a.src},#{a.seq})")
	int addAudio(@Param("a") AuserShopAudio entity);

}
